package com.example.menuvirtualclient.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.menuvirtualclient.R;
import com.example.menuvirtualclient.core.*;
import com.example.menuvirtualclient.presenter.*;


public class TableAdapter extends ArrayAdapter<Table>{

	Context context;
	int layoutResourceId;
	Table objects[] = null;
	float imageWidth;
	private TablePresenter _presenter;
	
	public TableAdapter(Context context, int layoutResourceId, Table[] objects) {
		super(context, layoutResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.objects = objects;
	}
	
	public TableAdapter(Context context, int layoutResourceId, List<Table> objects) {
		super(context, layoutResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.objects = objects.toArray(new Table[objects.size()]);
	}
	
	public TableAdapter(Context context, int layoutResourceId, Table[] objects, TablePresenter presenter) {
		super(context, layoutResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.objects = objects;
		_presenter = presenter;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ComponentsHolder holder;
		final int cc = position;

		// row = null; // This clears the view, so the rows are well drawn but
		// makes the app much slower

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ComponentsHolder();
			holder.id = (TextView) row.findViewById(R.id.tvTableId);
			holder.name = (TextView) row.findViewById(R.id.listMesas);
			holder.name = (TextView) row.findViewById(R.id.tvTableStatus);
			
			row.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//ver si corresponde agregar codigo					
				}
			});

			row.setTag(holder);
		} 
		else {
			holder = (ComponentsHolder) row.getTag();
			holder.id.setText("");
			holder.name.setText("");
			holder.status.setText("");
		}

		Table componentsList = objects[cc];

		holder.id.setText((String.valueOf(componentsList.getId())));
		holder.name.setText(componentsList.getName());
		holder.status.setText(componentsList.getStatus());

		return row;
	}

	static class ComponentsHolder {
		TextView id;
		TextView name;
		TextView status;
	}
	
}
